package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Music;
import com.qf.service.Impl.MusicServiceImpl;
import com.qf.service.MusicService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 音乐找到所有servlet
 *
 * @author 阿亘
 * @date 2023/04/20
 */
@WebServlet("/musicFindByNameServlet.action")
public class MusicFindByNameServlet extends HttpServlet {
    private MusicService musicService = new MusicServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前端传过来的参数
        String musicName = req.getParameter("musicName");
        //调用Service层模糊查询
        List<Music> musicList = musicService.findByName(musicName);
        //类型转换，转成Json类型
        String jsonMusicList = JSON.toJSONString(musicList);
        //返回给前端
        resp.getWriter().write(jsonMusicList);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
